package services;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import http.PackedPartedDownload;
import http.ParserConfig;
import http.RapidShareCookie;

public class DownloadListService {
	private String downloadTarget;
	private String rarExtractor;
	private List<PackedPartedDownload> downloads = new ArrayList<PackedPartedDownload>();
	public void setRarExtractor(String rarExtractorFile) {
		this.rarExtractor = rarExtractorFile;
	}
	public void setDownloadTarget(String downloadTarget) {
		this.downloadTarget = downloadTarget;
	}
	private static DownloadListService instance = null;
	private DownloadListService() {
	}
	public static DownloadListService getInstance() {
		if (instance == null)
			instance = new DownloadListService();
		return instance;
	}
	public void addPartedDownload(Map<String, String> downloadParts) {
		PackedPartedDownload download = new PackedPartedDownload();
		System.out.println("DownloadListService::addPartedDownload Register "+downloadParts.size()+" Downloads");
		for(Entry<String,String> e:downloadParts.entrySet())
			download.addDownload(e.getValue(), e.getKey());
		download.setTargetDirectory(downloadTarget);
		download.setRarExtractor(rarExtractor);
		download.setPassword(ParserConfig.getInstance().getConfig("archivepassword"));
		RapidShareCookie rs = new RapidShareCookie(
				ParserConfig.getInstance().getConfig("rapidshareusername"),
				ParserConfig.getInstance().getConfig("rapidsharepassword"));
		download.setCookies(rs.getCookie());
		download.start();
		System.out.println("DownloadListService::addPartedDownload download.start()");
		downloads.add(download);
	}
}
